package cn.kgc.hotel.service.impl;

import cn.kgc.hotel.dao.FoodMapper;
import cn.kgc.hotel.entity.Food;
import cn.kgc.hotel.entity.FoodReserve;
import cn.kgc.hotel.entity.ReserveFoodNum;
import cn.kgc.hotel.entity.Room;
import cn.kgc.hotel.service.FoodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Resource
    private FoodMapper foodMapper;

    @Override
    public List<Food> searchFoods() {
        List<Food> foodList = foodMapper.searchFoods();
        return foodList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean foodReserve(FoodReserve foodReserve, List<ReserveFoodNum> reserveFoodNumList) {
        int addFoodReserve = foodMapper.addFoodReserve(foodReserve);
        int updateFood = foodMapper.updateFood(reserveFoodNumList);
        int addReserveFoodNum = foodMapper.addReserveFoodNum(reserveFoodNumList);
        if (addFoodReserve > 0 && updateFood > 0 && addReserveFoodNum > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<FoodReserve> searchFoodReserve(int userId) {
        List<FoodReserve> foodReserveList = foodMapper.searchFoodReserve(userId);
        return foodReserveList;
    }

    @Override
    public boolean cancelFoodReserve(String reserveId) {
        int cancelFoodReserve = foodMapper.cancelFoodReserve(reserveId);
        if (cancelFoodReserve > 0) {
            return true;
        }
        return false;
    }
}
